using System;
using Engine;
using Game;

namespace RuthlessConquest {
    public class TextBoxDialog : Dialog {
        public Action<string> m_handler;

        public LabelWidget m_titleWidget;

        public TextBoxWidget m_textBoxWidget;

        public BevelledButtonWidget m_okButtonWidget;

        public BevelledButtonWidget m_cancelButtonWidget;

        public bool AutoHide { get; set; }

        public TextBoxDialog(string title, string text, int maximumLength, Action<string> handler) {
            m_handler = handler;
            CanvasWidget root = new() {
                Size = new Vector2(600f, 240f),
                ClampToBounds = true,
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Center
            };
            AddChildren(root);
            root.AddChildren(new RectangleWidget { FillColor = Color.Black, OutlineColor = new Color(128, 128, 128, 128), OutlineThickness = 2f });
            root.AddChildren(new InterlaceWidget());
            StackPanelWidget panel1 = new() {
                Direction = LayoutDirection.Vertical,
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Near,
                Margin = new Vector2(10f, 10f)
            };
            root.AddChildren(panel1);
            m_titleWidget = new LabelWidget {
                Name = "TextBoxDialog.Title",
                Color = Color.White,
                Margin = new Vector2(0f, 5f),
                FontScale = 0.75f,
                HorizontalAlignment = WidgetAlignment.Center
            };
            panel1.AddChildren(m_titleWidget);
            panel1.AddChildren(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            CanvasWidget canvas1 = new() { Margin = new Vector2(10f, 0f) };
            panel1.AddChildren(canvas1);
            canvas1.AddChildren(
                new RectangleWidget {
                    Size = new Vector2(float.PositiveInfinity, 40f), FillColor = Color.Transparent, OutlineColor = new Color(64, 64, 64, 64)
                }
            );
            m_textBoxWidget = new TextBoxWidget {
                Name = "TextBoxDialog.TextBox",
                Color = Color.White,
                Margin = new Vector2(4f, 0f),
                HorizontalAlignment = WidgetAlignment.Stretch,
                VerticalAlignment = WidgetAlignment.Center
            };
            canvas1.AddChildren(m_textBoxWidget);
            panel1.AddChildren(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            StackPanelWidget panel2 = new() { Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Center };
            panel1.AddChildren(panel2);
            m_okButtonWidget = new BevelledButtonWidget { Name = "TextBoxDialog.OkButton", Text = LanguageControl.Ok, Size = new Vector2(200f, 60f) };
            panel2.AddChildren(m_okButtonWidget);
            panel2.AddChildren(new CanvasWidget { Size = new Vector2(40f, 0f) });
            m_cancelButtonWidget = new BevelledButtonWidget {
                Name = "TextBoxDialog.CancelButton", Text = LanguageControl.Cancel, Size = new Vector2(200f, 60f)
            };
            panel2.AddChildren(m_cancelButtonWidget);
            //XElement node = ContentManager.Get<XElement>("RCDialogs/TextBoxDialog");
            //LoadContents(this, node);
            //m_titleWidget = Children.Find<LabelWidget>("TextBoxDialog.Title");
            //m_textBoxWidget = Children.Find<TextBoxWidget>("TextBoxDialog.TextBox");
            //m_okButtonWidget = Children.Find<BevelledButtonWidget>("TextBoxDialog.OkButton");
            //m_cancelButtonWidget = Children.Find<BevelledButtonWidget>("TextBoxDialog.CancelButton");
            m_okButtonWidget.Sound = "RCSounds/Click2";
            //m_okButtonWidget.IsOkButton = true;
            m_cancelButtonWidget.Sound = "RCSounds/Click2";
            //m_cancelButtonWidget.IsCancelButton = true;
            m_titleWidget.IsVisible = !string.IsNullOrEmpty(title);
            m_titleWidget.Text = title ?? string.Empty;
            m_textBoxWidget.MaximumLength = maximumLength;
            m_textBoxWidget.Text = text ?? string.Empty;
            m_textBoxWidget.Title = title;
            m_textBoxWidget.Title = title;
            m_textBoxWidget.HasFocus = true;
            AutoHide = true;
        }

        public override void Update() {
            if (Input.Cancel
                || (Input.Tap.HasValue && !HitTest(Input.Tap.Value))) {
                Dismiss(null);
            }
            else if (Input.Ok) {
                Dismiss(m_textBoxWidget.Text);
            }
            else if (m_okButtonWidget.IsClicked) {
                Dismiss(m_textBoxWidget.Text);
            }
            else if (m_cancelButtonWidget.IsClicked
                || (Input.Tap.HasValue && !HitTest(Input.Tap.Value))) {
                Dismiss(null);
            }
        }

        public void Dismiss(string result) {
            if (AutoHide) {
                DialogsManager.HideDialog(this);
            }
            m_handler?.Invoke(result);
        }
    }
}